#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 300560;
struct node
{
    int u, v, cost;
} s[maxn];
int par[maxn];
int ran[maxn];
int n, m;
void init(int n)
{
    for (int i = 1; i <= n; i++)
    {
        par[i] = i;
        ran[i] = 1;
    }
}
bool cmp(node a, node b)
{
    return a.cost < b.cost;
}
int find(int x)
{
    return x == par[x] ? x : find(par[x]);
}
void unite(int x, int y)
{
    x = find(x);
    y = find(y);
    if (x == y)
        return;
    if (ran[x] < ran[y])
    {
        par[x] = y;
    }
    else
    {
        par[y] = x;
        if (ran[x] == ran[y])
            ran[x]++;
    }
}
bool same(int a, int b)
{
    return find(a) == find(b);
}
bool jud(int n)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
    {
        if (par[i] == i)
            res++;
    }
    return res == 1;
}
int main()
{
    while (scanf("%d %d", &n, &m) && (n + m))
    {
        int ans = INF;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d %d %d", &s[i].u, &s[i].v, &s[i].cost);
        }
        sort(s + 1, s + m + 1, cmp);
        int flag = 0;
        for (int i = 1; i <= m; i++)
        {
            init(n);
            for (int j = i; j <= m; j++)
            {
                if (!same(s[j].u, s[j].v))
                {
                    unite(s[j].u, s[j].v);
                    if (jud(n))
                    {
                        ans = min(ans, s[j].cost - s[i].cost);
                        flag = 1;
                    }
                }
            }
        }
        if (!flag)
            puts("-1");
        else
            printf("%d\n", ans);
    }
}